package com.sr.examination.exam.exercise.category;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.sr.examination.R;
import com.tl.commonlibrary.tool.MathUtil;
import com.tl.commonlibrary.ui.beans.ExerciseBean;
import com.tl.commonlibrary.ui.beans.ExerciseOption;
import com.tl.commonlibrary.ui.widget.NumberEditText;

import java.util.List;

/**
 * Created by jiangzhongshan on 18/2/3.
 * 简答题
 */

public abstract class SubjectiveFragment extends ExerciseFragment {

    private EditText myAnswerEText;
    private NumberEditText myScoreEText;
    private View myScoreLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.frag_short_answer_exercise, null);
        root.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    hiddenKeyboard();
                }
                return false;
            }
        });
        myAnswerEText = root.findViewById(R.id.myAnswerEText);
        myScoreLayout = root.findViewById(R.id.myScoreLayout);
        myScoreEText = root.findViewById(R.id.myScoreEText);
        myScoreEText.setMaxValue(exerciseBean.getScore() + "");
        super.onCreateView(inflater, container, savedInstanceState);
        init();
        return root;
    }


    protected void init() {
        String userAnswer = exerciseBean.getUserAnswer();
        if (!TextUtils.isEmpty(userAnswer)) {
            exerciseBean.setAnswered(true);
            setMyAnswer(exerciseBean.getUserAnswer());
        }
    }


    @Override
    protected void onExerciseAnalysisModeCalled() {
        super.onExerciseAnalysisModeCalled();
        setAnswer(exerciseBean.getAnswer());
        //我输入的答案
        myAnswerEText.setVisibility(View.GONE);
        myScoreLayout.setVisibility(View.GONE);
    }

    @Override
    protected void onExerciseAnsweringModeCalled() {
        super.onExerciseAnsweringModeCalled();
        //我输入的答案
        myAnswerEText.setVisibility(View.VISIBLE);
        myScoreLayout.setVisibility(View.GONE);
    }

    @Override
    protected void onExerciseAnalysisMarkModeCalled() {
        super.onExerciseAnalysisMarkModeCalled();
        myScoreLayout.setVisibility(View.VISIBLE);
    }

    protected void setMyAnswer(String myAnswer) {
        myAnswerEText.setText(myAnswer);
    }

    @Override
    public void saveExerciseInstanceState() {
        super.saveExerciseInstanceState();
        exerciseBean.setUserAnswer(myAnswerEText.getText().toString().trim());
        if (exerciseBean.getDisplayMode() == ExerciseBean.DISPLAY_MODE_ANALYSIS_MARK) {
            String myScore = myScoreEText.getText().toString().trim();
            if (MathUtil.isDouble(myScore)) {
                exerciseBean.setMyScore(MathUtil.toDouble(myScore));
            }
        }
    }

    @Override
    protected void onClickOption(List<ExerciseOption> options) {

    }

    @Override
    public void onClick(View v) {

    }
}
